package admin.dao.impl;

import admin.bean.Eadmin;
import admin.dao.EadminDao;
import exception.ReCodeException;
import util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EadminDaoImpl extends DBUtil implements EadminDao {

    public static final String SQL_CONSOLE = " select count(*) totalnum," +
            "count(to_days(createtime)=to_days(now()) or null) daynum from eadmin ";

    public static final String SQL_FIND_ALL = " select * from eadmin ";

    public static final String SQL_FIND_LIMIT = " select * from eadmin limit ?,? ";

    public static final String SQL_INSERT = " insert into eadmin (username,password,realname,phone,IDCard,createtime) VALUES (?,?,?,?,?,now()) ";

    public static final String SQL_FIND_BY_PHONE = " select id from eadmin where phone=? ";

    public static final String SQL_UPDATE_BY_ID = " update eadmin set realname=?,phone=?,IDCard=?,username=?,password=? where id=? ";

    public static final String SQL_DELETE_BY_PHONE = " delete from eadmin where phone=? ";

    @Override
    public Map<String, Integer> console() {
        Map<String,Integer> map = new HashMap<>();

        try {
            resultSet = query(SQL_CONSOLE);
            if (resultSet.next()){
                int totalNum = resultSet.getInt("totalnum");
                int dayNum = resultSet.getInt("daynum");
                map.put("totalNum",totalNum);
                map.put("dayNum",dayNum);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return map;
    }

    @Override
    public List<Eadmin> findAll(boolean limit, int offset, int pageNumber) {
        List<Eadmin> list = new ArrayList<>();

        ArrayList params = new ArrayList();
        params.add(offset);
        params.add(pageNumber);


        try {
            if (limit){
                resultSet = query(SQL_FIND_LIMIT,params);
            }else {
                resultSet = query(SQL_FIND_ALL);
            }

            while (resultSet.next()){
                Eadmin eadmin = new Eadmin();
                eadmin.setId(resultSet.getInt("id"));
                eadmin.setUserName(resultSet.getString("username"));
                eadmin.setPassWord(resultSet.getString("password"));
                eadmin.setRealName(resultSet.getString("realname"));
                eadmin.setPhone(resultSet.getString("phone"));
                eadmin.setIDCard(resultSet.getString("IDCard"));
                eadmin.setLoginTime(resultSet.getTimestamp("logintime"));
                eadmin.setLogoutTime(resultSet.getTimestamp("logouttime"));
                eadmin.setCreateTime(resultSet.getTimestamp("createtime"));
                eadmin.setLoginIP(resultSet.getString("loginip"));

                list.add(eadmin);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return list;
    }

    @Override
    public int findByPhone(String phone) {
        ArrayList params = new ArrayList();
        params.add(phone);
        int id = 0;

        try {
            resultSet = query(SQL_FIND_BY_PHONE,params);
            //只有一个结果，直接有if
            if (resultSet.next()){
                id = resultSet.getInt("id");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return id;
    }

    @Override
    public boolean insert(Eadmin eadmin) throws ReCodeException {
        ArrayList params = new ArrayList();
        params.add(eadmin.getUserName());
        params.add(eadmin.getPassWord());
        params.add(eadmin.getRealName());
        params.add(eadmin.getPhone());
        params.add(eadmin.getIDCard());

        try {
            int rows = update(SQL_INSERT,params);

            return rows > 0;
        } catch (ReCodeException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return false;
    }

    @Override
    public boolean update(int id, Eadmin newEadmin) throws ReCodeException {
        ArrayList params = new ArrayList();
        params.add(newEadmin.getRealName());
        params.add(newEadmin.getPhone());
        params.add(newEadmin.getIDCard());
        params.add(newEadmin.getUserName());
        params.add(newEadmin.getPassWord());
        params.add(id);

        try {
            int rows = update(SQL_UPDATE_BY_ID,params);
            return rows >0;
        } catch (ReCodeException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return false;
    }

    @Override
    public boolean delete(String phone) {
        ArrayList params = new ArrayList();
        params.add(phone);

        try {
            int rows = update(SQL_DELETE_BY_PHONE,params);

            return rows > 0;
        } catch (ReCodeException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return false;
    }
}
